<?php

namespace application;

use fastphp\Controller;
use fastphp\Validator;

class BaseController extends Controller
{

    /**
     * 统一的数据验证方法
     */
    protected function validateData(array $data, array $rules): bool
    {
        if (!Validator::make()->validate($data, $rules)) {
            $errors = Validator::make()->getErrors();
            $this->error(implode(', ', $errors));
            return false;
        }
        return true;
    }

    /**
     * 成功响应
     */
    protected function success(array $data = [], string $msg = '操作成功'): void
    {
        $this->json($msg, $data, 200);
    }

    /**
     * 错误响应
     */
    protected function error(string $msg = '操作失败', int $code = 400): void
    {
        $this->json($msg, [], $code);
    }
}
